#include "stdafx.h"
#include "JSParse.h"
#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Shell.hxx>

#include <TopExp_Explorer.hxx>

#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>

#include <BRepMesh_IncrementalMesh.hxx>
#include <BRepMesh.hxx>
#include <BRep_Tool.hxx>
#include <QFile>
#include <Poly_Triangulation.hxx>
#include <TShort_Array1OfShortReal.hxx>

#include "BasicObject.h"

#include "UIClass.h"

ZMString JSParse::m_strFuncCreateAndUpdateObject = "CreateAndUpdateObject";
JSParse::JSParse(ZMString strJSFileName):ScriptBase(strJSFileName),
m_strJSFileName(strJSFileName)
{
	QFile file(ToQString(m_strJSFileName));
	QString strData;
	file.open(QIODevice::ReadOnly | QIODevice::Text);
	if (file.isOpen())
	{
		QByteArray data = file.readAll();
		strData = QString(data);
		file.close();
	}
	if (!strData.isEmpty())
	{
		JSValue occCreate = m_mainEngine.newQObject(&m_occCreateObj);
		m_mainEngine.globalObject().setProperty("OCCCreate", occCreate);

		JSValue occAlgo = m_mainEngine.newQObject(&m_occAlgo);
		m_mainEngine.globalObject().setProperty("OCCAlgo", occAlgo);

		JSValue occOperate = m_mainEngine.newQObject(&m_occOperate);
		m_mainEngine.globalObject().setProperty("OCCOperate", occOperate);

	//	m_mainEngine.evaluate(strData, m_strJSFileName);
		m_mainEngine.evaluate(strData, ToQString(m_strJSFileName));
	}
#ifndef USE_JSENGINE
	 bool isOpenDebug = false;
	if (!isOpenDebug)
	{
		m_Debugger.attachTo(&m_mainEngine);
		//m_Debugger.evaluationSuspended();
		//m_Debugger.setAutoShowStandardWindow(true);
		//	m_debugWindow = m_Debugger.standardWindow();
		//	m_debugWindow->resize(1024, 640);
		//	m_debugWindow->show();
		isOpenDebug = true;
	}
#endif // !USE_JSENGINE

}

bool JSParse::IsVaid() const
{
	QFile file(ToQString(m_strJSFileName));
	file.open(QIODevice::ReadOnly | QIODevice::Text);
	return file.isOpen();
}


JSParse::~JSParse()
{
}


ZMVector<MeshObjectBase::suDrawMesh> JSParse::GetMeshData(const ZMString& strMeshName)
{
	ZMVector<suDrawMesh> ret;
	suDrawMesh drawMesh;
	ZMString strNodeName = strMeshName == "" ? GetObjName() : strMeshName;

	JSValue jsShape = m_mainEngine.evaluate(ToQString(m_strFuncCreateAndUpdateObject), ToQString(m_strJSFileName));
	if (0)
	{
		m_debugWindow = m_Debugger.standardWindow();
		m_debugWindow->show();
	}
	QVariant varShap = jsShape.call().toVariant();

	TopoDS_Shape shape = varShap.value<TopoDS_Shape>();

	ZMVector<ZMPoint3D> vPnt;
	ZMVector<ZMPoint3D> vNormal;
	ZMVector<int> vTriIndex;
	ZMVector<ZMPoint3D> vTangentData;
	TriangleShape(shape, vPnt, vNormal, vTangentData, vTriIndex);

	drawMesh.m_strMatName = "";
	drawMesh.m_strNodeName = strNodeName;
	drawMesh.m_pMeshData = SharedPointer<MeshData>(new MeshData());
	drawMesh.m_pMeshData->SetPntData(vPnt);
	drawMesh.m_pMeshData->SetTriIndex(vTriIndex);
	drawMesh.m_pMeshData->SetNormalData(vNormal);
	ret.push_back(drawMesh);
	return ret;
}

void JSParse::SetParamCore(const ZMVector<SharedPointer<UIDataBase>>& vParam)
{
	if (IsVaid())
	{
		for (SharedPointer<UIDataBase> pData : vParam)
		{
			if (pData->GetVarDesti() == UIDataBase::TO_JS)
			{
				string strProname = ToString(pData->GetID());
				QVariant varValue = pData->GetValue();
				JSValue jsData = m_mainEngine.newVariant(varValue);
				m_mainEngine.globalObject().setProperty(strProname.c_str(), jsData);
			}
		}
	}
}

